<template>
  <div class="user-profile">
    <!-- 导航栏 -->
    <van-nav-bar
      class="page-nav-bar"
      title="个人信息"
      left-arrow
      @click-left="$router.back()"
    >
    </van-nav-bar>
    <input type="file" hidden ref="file" @change="onFileChange" />
    <!-- 个人信息 -->
    <van-cell title="头像" is-link>
      <van-image
        class="avatar"
        fit="cover"
        round
        :src="user.photo"
        @click="$refs.file.click()"
      />
    </van-cell>
    <van-cell
      @click="isUpdateNameShow = true"
      title="昵称"
      :value="user.name"
      is-link
    ></van-cell>
    <van-cell
      title="性别"
      :value="user.gender === 1 ? '女' : '男'"
      is-link
      @click="isUpdateGenderShow = true"
    ></van-cell>
    <van-cell
      title="生日"
      :value="user.birthday"
      is-link
      @click="isUpdateBirthdayShow = true"
    ></van-cell>

    <!-- 编辑昵称弹出层 -->
    <van-popup
      position="bottom"
      v-model="isUpdateNameShow"
      style="height: 100%"
    >
      <update-name
        v-if="isUpdateNameShow"
        v-model="user.name"
        @close="isUpdateNameShow = false"
      ></update-name>
    </van-popup>
    <!-- 编辑性别弹出层 -->
    <van-popup
      position="bottom"
      v-model="isUpdateGenderShow"
      style="height: 100%"
    >
      <update-gender
        v-if="isUpdateGenderShow"
        v-model="user.gender"
        @close="isUpdateGenderShow = false"
      ></update-gender>
    </van-popup>
    <!-- 编辑生日弹出层 -->
    <van-popup
      position="bottom"
      v-model="isUpdateBirthdayShow"
      style="height: 30%"
    >
      <update-birthday
        v-if="isUpdateBirthdayShow"
        v-model="user.birthday"
        @close="isUpdateBirthdayShow = false"
      ></update-birthday>
    </van-popup>
    <!-- 编辑头像弹出层 -->
    <van-popup
      position="bottom"
      v-model="isUpdatePhotoShow"
      style="height: 100%"
    >
      <update-photo
        v-if="isUpdatePhotoShow"
        :img="img"
        @close="isUpdatePhotoShow = false"
        @updataPhoto="user.photo = $event"
      ></update-photo>
    </van-popup>
  </div>
</template>

<script>
import { getUserProfileAPI } from '@/api'
import UpdateName from './components/update-name.vue'
import UpdateGender from './components/update-gender.vue'
import UpdateBirthday from './components/update-birthday.vue'
import UpdatePhoto from './components/update-photo.vue'
export default {
  name: 'userProfile',
  data () {
    return {
      user: {}, // 个人信息
      isUpdateNameShow: false, // 控制修改昵称弹出框的显示与隐藏
      isUpdateGenderShow: false, // 控制修改性别弹出框的显示与隐藏
      isUpdateBirthdayShow: false, // 控制修改生日弹出框的显示与隐藏
      isUpdatePhotoShow: false, // 控制修改头像弹出框的显示与隐藏
      img: null // 预览的图片
    }
  },
  components: {
    UpdateName,
    UpdateGender,
    UpdateBirthday,
    UpdatePhoto
  },
  created () {
    this.loadUserProfile()
  },
  methods: {
    async loadUserProfile () {
      try {
        const { data } = await getUserProfileAPI()
        this.user = data.data
      } catch (error) {
        this.$toast('获取用户信息失败！')
      }
    },
    // 修改头像事件处理函数
    onFileChange () {
      // 获取文件对象
      const file = this.$refs.file.files[0]
      // 基于文件对象获取 blob 数据
      this.img = window.URL.createObjectURL(file)
      //   展示预览图片弹出层
      this.isUpdatePhotoShow = true
      //   file-input如果选了同一个文件不会触发change事件
      // 解决办法就是每次使用完毕，把它的value清空
      this.$refs.file.value = ''
    }
  }
}
</script>

<style lang="less" scoped>
.user-profile {
  /deep/.van-nav-bar__left {
    .van-icon-arrow-left {
      color: #fff;
    }
  }
  .avatar {
    width: 60px;
    height: 60px;
  }
  .van-popup {
    background-color: #f5f7f9;
  }
}
</style>
